/*
Audica Uploader - application for convenient uploading to various file hostings
Copyright (C) 2011-2012 Makarov Andrey (Winand)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "zpushbutton.h"
#include <QDebug>
#include <QEvent>

ZPushButton::ZPushButton(QWidget* parent) :
    QPushButton(parent), timer(0), waiting(0), p_confirmation(true)
{
    connect(this, SIGNAL(clicked(bool)), this, SLOT(p_clicked(bool)));
}

void ZPushButton::setText(const QString &text) {
    if(!waiting) QPushButton::setText(text);
    p_text = text;
}

void ZPushButton::timerEvent(QTimerEvent* /*e*/) {
    waiting--;
    if(waiting)
        QPushButton::setText(tr("Confirm: %1").arg(waiting));
    else {
        QPushButton::setText(p_text);
        killTimer(timer);
    }
}

void ZPushButton::p_clicked(bool /*checked*/) {
    if(!p_confirmation) return; //no confirmation
    if(waiting) {
        QPushButton::setText(p_text);
        killTimer(timer);
        waiting = 0;
        emit confirmed();
    } else {
        timer = startTimer(1000);
        waiting = 3;
        QPushButton::setText(tr("Confirm: %1").arg(waiting));
    }
}
